'use strict'

import { app, BrowserWindow, ipcMain } from 'electron'
import { exec } from 'child_process'
// import util from 'util'
// import iconv from 'iconv-lite'
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow = null
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  // #region  只允许启动一次
  //* 释放所有由 makeSingleInstance 创建的限制. 这将允许应用程序的多个实例依次运行.
  // app.releaseSingleInstance()
  const isSecondInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
    if (mainWindow) {
      if (mainWindow.isMaximized()) {
        mainWindow.restore()
        mainWindow.focus()
      }
    }
  })

  if (isSecondInstance) {
    app.quit()
  }
  // #endregion

  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 500,
    width: 400,
    resizable: false,
    frame: false,
    transparent: true,
    fullscreen: false,
    maximizable: false
    // alwaysOnTop: true
  })
  mainWindow.once('ready-to-show', () => {
    mainWindow.show()
  })

  mainWindow.loadURL(winURL)
  // BrowserWindow.addDevToolsExtension('node_modules/vue-devtools/vender')
  // BrowserWindow.addDevToolsExtension('node_modules/devtron')
  mainWindow.on('closed', () => {
    mainWindow = null
  })

  //! ipc 通信
  ipcMain.on('get-app-versions', (event, arg) => {
    // console.log(arg)
    event.sender.send('got-app-versions', app.getVersion())
  })

  ipcMain.on('get-app-name', (event, arg) => {
    // console.log(arg)
    event.sender.send('got-app-name', app.getName())
  })
  //! 响应关机命令
  ipcMain.on('set-timing-shutdown', (event, arg) => {
    console.log(arg)
    switch (require('os').platform()) {
      case 'win32':
        exec(`shutdown -s -t 1000`)
        break
      case 'darwin':
        exec(`sudo shutdown -r now `)
        break
      case 'linux':
        exec(`sudo shutdown -r now `)
        break
      default:
        event.sender.send('got-timing-message', '不支持')
        break
    }
  })
  //! 取消命令
  ipcMain.on('set-end-shutdown', (event, arg) => {
    switch (require('os').platform()) {
      case 'win32':
        exec(`shutdown -a`)
        break
      case 'darwin':
        exec(`sudo shutdown -c`)
        break
      case 'linux':
        exec(`sudo shutdown -c`)
        break
      default:
        event.sender.send('got-end-message', '不支持')
        break
    }
  })
}
app.setName('SEV关机助手')
app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
